'use strict';

import React, { Component } from 'react';
import { Text, StyleSheet, View, ScrollView, RefreshControl } from 'react-native';
import ListViewForHomeTab from '../component/ListViewForHome';
import ListViewForOtherTab from '../component/ListViewForOther';
// import computeTime from '../../util/computeTime';
import theme from '../config/Theme';

export default class HomeTab extends Component {
    constructor(props) {
        super(props);
        this.state = {
            refreshing: true,
            loadedData: false,
            dataBlob: []
        };
    }

    componentDidMount() {
        this._fetchData();
        // this._MockData()
    }

    _MockData() {
        var dataBlob = [];
        for (let i = 0; i < 15; i++) {
            let info = {
                title: "测试",
                url: "http://pic32.nipic.com/20130828/12296369_131236125000_2.jpg",
            }
            dataBlob.push(info);
        }
        console.log("列表数量：" + dataBlob.length)
        if (dataBlob.length !== 0) {
            this.setState({
                dataBlob: dataBlob,
                loadedData: true,
                refreshing: false
            });
        }
    }

    render() {
        return (
            <ScrollView
                style={{}}
                refreshControl={
                    <RefreshControl
                        refreshing={this.state.refreshing}
                        onRefresh={this._onRefresh.bind(this)}
                        colors={['red', '#ffd500', '#0080ff', '#99e600']}
                        tintColor={theme.themeColor}
                        title="Loading..."
                        titleColor={theme.themeColor}
                    />
                }>
                {this._renderContents()}
            </ScrollView>
        );
    }

    _renderContents() {
        var {tabTag} = this.props;
        if (!this.state.refreshing || this.state.loadedData) {
           
            return (
                <View>
                    {tabTag === '推荐' ?
                        <ListViewForHomeTab contents={this.state.dataBlob} navigator={this.props.navigator}/>
                        :
                        <ListViewForOtherTab contents={this.state.dataBlob} navigator={this.props.navigator}/>
                    }
                </View>
            );
        }
    }

    _onRefresh() {
        this.setState({ refreshing: true });
        // this._fetchData();
        this._MockData();
    }

    _getCurrentTime() {
        function convertTime(time) {
            if (time <= 9)
                return '0' + time;
            return time;
        }

        var date = new Date();
        return date.getFullYear() + '-' + convertTime(date.getMonth() + 1) + '-' + convertTime(date.getDate()) + 'T' + convertTime(date.getHours()) + ':' + convertTime(date.getMinutes()) + ':' + convertTime(date.getSeconds() + '.' + date.getMilliseconds() + 'Z');
    }

    _fetchData() {
        var url = 'http://www.wxspider.com/chuangfubao/rs/adShareInterfaces/getWebs';
        fetch(url, {
            method: 'POST',
            headers: {
                'Accept': 'application/json',
                'Content-Type': 'application/json',
            },
            body: JSON.stringify({
                pageNo: '1',
                pageSize: '10',
            })
        })
            .then((response) => {
              return response.json()//注意这里要写return，官方文档里面没写导致出错
            })
            .then((responseData) => {
                let data = responseData.data;
                var dataBlob = [];

                for (let i in data) {
                    let info = {
                        title: data[i].title,
                        url: data[i].url,
                        title_pic: data[i].title_pic,
                        modifyTime: data[i].modifyTime
                    }
                    dataBlob.push(info);
                }

                if (dataBlob.length !== 0) {
                    this.setState({
                        dataBlob: dataBlob,
                        loadedData: true,
                        refreshing: false
                    });
                }
            }).done();
    }
}
